/**
  * 联系客服
  */
module game {
    export class KeFuMediator extends BaseMediator {
        public static NAME: string = "KeFuMediator";
        private gameType: string = "";
        public constructor(viewComponent: any = null) {
            super(KeFuMediator.NAME, viewComponent);
        }

        public listNotificationInterests(): Array<any> {
            return [
                PanelNotify.HALL.STAGE_RESIZE,
                PanelNotify.HALL.OPEN_KEFU,
                PanelNotify.HALL.CLOSE_KEFU
            ];
        }
        private panel: KeFuPanel;
        public handleNotification(notification: puremvc.INotification): void {
            this.gameType = notification.getBody();
            switch (notification.getName()) {
                case PanelNotify.HALL.STAGE_RESIZE:
                    if (this.panel) {
                        this.panel.width = GameConfig.curWidth()
                        this.panel.height = GameConfig.curHeight()
                    }
                    break;
                case PanelNotify.HALL.OPEN_KEFU: {
                    this.panel = null;
                    this.panel = new KeFuPanel();
                    this.showUI(this.panel, 2, 1, true);
                    break;
                }
                case PanelNotify.HALL.CLOSE_KEFU: {
                    this.closePanel(0);
                    break;
                }
            }
        }

        /**
         * 初始化面板ui
         */
        public initUI(): void {
            this.panel.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.closeButtonClick, this);
            this.panel.cjwtBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.typeClick, this);
            this.panel.yjfkBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.typeClick, this);
            this.panel.lianxikefu.addEventListener(egret.TouchEvent.TOUCH_TAP, this.lxkfClick, this);
            this.panel.confirm.addEventListener(egret.TouchEvent.TOUCH_TAP, this.confirmBrokeButtonClick, this);
            
        }

        /**
         * 初始化面板数据
         */
        public initData(): void {
            this.typeClick(null)
        }
        //切换标签页
        private typeClick(event: egret.TouchEvent) {
            this.panel.cjwtBtn.touchEnabled = true
            this.panel.yjfkBtn.touchEnabled = true

            this.panel.cjwtBtn.icon = "kefu_cjwt"
            this.panel.yjfkBtn.icon = "kefu_yj"

            this.panel.cjwt_view.visible = false
            this.panel.fankui_view.visible = false

            if (event == null || event.currentTarget == this.panel.cjwtBtn) {
                this.panel.cjwtBtn.touchEnabled = false
                this.panel.cjwtBtn.icon = "kefu_cjwt_x"
                this.panel.cjwt_view.visible = true
            } else if (event.currentTarget == this.panel.yjfkBtn) {
                this.panel.yjfkBtn.touchEnabled = false
                this.panel.yjfkBtn.icon = "kefu_yj_x"
                this.panel.fankui_view.visible = true
            }
        }

        private closeButtonClick(event: egret.TouchEvent): void {
            SoundManage.startAnniuBtnLoad();
            this.closePanel(1);
        }
        private lxkfClick() {
            SoundManage.startAnniuBtnLoad();
            var time = Global.getCurrTimeLong()
            var signature = Global.getSynLoginKey(time)
            signature = signature.toUpperCase()
            var request = new egret.HttpRequest();
            request.responseType = egret.HttpResponseType.TEXT;
            request.open(InstructionsEnum.IP_API + "/H5Interface/GetOnLineCS?userid=" + GameVO.getVo().dwUserID + "&signature=" + signature + "&time=" + time, egret.HttpMethod.POST);
            request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            request.send();
            request.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
                try {
                    var request = <egret.HttpRequest>event.currentTarget;
                    var obj = JSON.parse(request.response)
                    if (obj.code == 0) {
                        var url = obj["data"]
                        window.open(url)
                    } else {
                        Global.tipShow(obj.msg)
                    }
                } catch (err) {
                    EffectUtils.textFadeAway("数据包异常，无法解析!")
                }
            }, this);
        }

        private confirmBrokeButtonClick(event: egret.TouchEvent): void {
            SoundManage.startAnniuBtnLoad();
            var text: string = this.panel.text.text;
            if (text == "") {
                EffectUtils.textFadeAway("提交内容必须为10-60个字符")//弹出提示消息
                return;
            }
            // 去除所有空格:
            // str = str.replace(/\s+/g, "");
            // 去除两头空格:
            // str = str.replace(/^\s+|\s+$/g, "");
            var text = text.replace(/^\s+|\s+$/g, "");
            if (text.length < 10 || text.length > 60) {
                EffectUtils.textFadeAway("提交内容必须为10-60个字符")//弹出提示消息
                return;
            }
            var url = InstructionsEnum.IP_API + "/GameWebApplication/SubmitMessage.ashx"
            var httpRequest = new XMLHttpRequest();
            httpRequest.onreadystatechange = function () {
                if (httpRequest.readyState == 4 && httpRequest.status == 200) {
                    EffectUtils.textFadeAway("感谢您的反馈！我们会继续改进！")//弹出提示消息
                }
            };
            httpRequest.open('POST', url, true);
            httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            // httpRequest.setRequestHeader("Access-Control-Allow-Credentials", "true");
            httpRequest.send(JSON.stringify({
                "MsgType": 0,
                "MsgTiltle": "反馈意见",
                "MsgContent": text,
                "UserId": GameVO.getVo().dwUserID
            }));
            this.closePanel(1);
        }
    }
}
